package com.example.kotlin.model

import com.example.kotlin.handler.*
import com.example.kotlin.proxy.*
import xyz.erupt.annotation.*
import xyz.erupt.annotation.sub_field.*
import xyz.erupt.annotation.sub_field.sub_edit.*
import xyz.erupt.jpa.model.*
import javax.persistence.*

@Erupt(name = "书籍", dataProxy = [BookDataProxy::class])
@Table(name = "t_book")
@Entity
class Book : BaseModel() {

    @EruptField(
        views = [View(title = "书名")],
        edit = Edit(title = "书名", type = EditType.INPUT, notNull = true, inputType = InputType()),
    )
    var name: String = ""

    @EruptField(views = [View(title = "图书类别", sortable = true, column = "name")])
    @ManyToOne
    var category: Category? = null

    @EruptField(
        edit = Edit(
            title = "图书类别",
            type = EditType.CHOICE,
            choiceType = ChoiceType(
                type = ChoiceType.Type.SELECT,
                fetchHandler = [GetAllCategories::class],//在下拉框中列出所有的图书类别,
            ),
        ),
    )
    @Transient // categoryId是个临时字段,不会持久化到数据库中
    var categoryId: Long? = null

}
